/**
 * @file 查询优惠
 * @author wangxiaokun
 * @date 2018/12/14 16:38:57
 */

layui.define(['table'], function (exports) {
    //var zmnTable = layui.zmnTable;
    var table = layui.table;
    var options;
    var promotionTable;
    var discountTable;
    var thirdDiscountTable;
    var incrementTable;
    var userBindTable;

    /**
     * 渲染增值优惠表格
     * @returns {object}
     */
    function renderIncrementTable() {
        incrementTable = table.render({
            elem: "#zmn-increment-table",
            width: 1096,
            data: [],
            cols: [[
                { field: 'incrementProductName', title: '关联增值产品名称', width:122, align: 'center' },
                { field: 'incrementProductPrice', title: '关联增值产品售价', width:122, align: 'center' },
                { field: 'originalPrice', title: '关联增值产品原价', width:122, align: 'center'},
                { field: 'discountType', title: '优惠券类型', width:80, align: 'center'},
                { field: 'quota', title: '额度', width:70, align: 'center'},
                { field: 'useCondition', title: '使用条件', width:100, align: 'center'},
                { field: 'periodValidity', title: '有效期', width:300, align: 'center'},
                { field: 'desc', title: '说明', width:179, align: 'center'}
            ]],
            done: function () {},
            even: true,
        });
    }

    /**
     * 渲染促销优惠表格
     * @returns {object}
     */
    function renderPromotionTable() {
        promotionTable = table.render({
            elem: "#zmn-promotion-table",
            width: 1096,
            data: [],
            cols: [[
                { field: 'discountTypeName', title: '优惠方式', width:70, align: 'center', templet: function (d) {
                        return '<span style="background-color:#FF6600">' + d.discountTypeName + '</span>';
                    } },
                { field: 'discountInfo', title: '减免力度', width:170, align: 'center' },
                { field: 'discountTime', title: '优惠时间', width:300, align: 'center'},
                { field: 'desc', title: '说明', width:551, align: 'center'}
            ]],
            done: function () {},
            even: true,
        });
    }

    /**
     * 渲染第三方活动优惠表格
     * @returns {object}
     */
    function renderThirdDisountTable() {
        thirdDiscountTable = table.render({
            elem: "#zmn-third-discount",
            width: 1096,
            data: [],
            cols: [[
                { field: 'desc', title: '活动说明', width:1094, align: 'center' }
            ]],
            done: function () {},
            even: true,
        });
    }

    /**
     * 渲染用户优惠表格
     * @returns {object}
     */
    function renderDiscountTable() {
        discountTable = table.render({
            elem: "#zmn-discount-table",
            width: 1096,
            data: [],
            cols: [[
                { field: 'discountTypeName', title: '优惠券类型', width:100, align: 'center' },
                { field: 'quota', title: '额度', width:100, align: 'center'},
                { field: 'useCondition', title: '使用条件', width:150, align: 'center'},
                { field: 'periodValidity', title: '有效期', width:290, align: 'center'},
                { field: 'desc', title: '说明', width:449, align: 'center'}
            ]],
            done: function () {},
            even: true
        });
    }

    /**
     * 渲染用户绑定优惠表格
     * @returns {object}
     */
    function renderUserBindDiscountTable() {
        userBindTable = table.render({
            elem: "#zmn-bind-table",
            width: 1096,
            data: [],
            cols: [[
                { field: 'discountTypeName', title: '优惠券类型', width:200, align: 'center' },
                { field: 'batchName', title: '批次名称', width:200, align: 'center' },
                { field: 'quota', title: '面额/折扣', width:100, align: 'center'},
                { field: 'periodValidity', title: '有效期', width:595, align: 'center'}
            ]],
            done: function () {},
            even: true
        });
    }

    /**
     * 初始化页面
     */
    function initPage () {
        options = parent.window.showDiscountOptions;
        if (options == undefined) {
            return;
        }
        if (options.promotionDiscountList != undefined && options.promotionDiscountList.length != 0){
            renderPromotionTable();
            promotionTable.reload({
                data: options.promotionDiscountList,
            })
        } else {
            $("#noPromotion").text("无")
        }
        if (options.userDiscountList  != undefined && options.userDiscountList.length != 0){
            renderDiscountTable();
            discountTable.reload({
                data: options.userDiscountList,
            })
        } else {
            $("#noDiscount").text("无")
        }
        if (options.thirdDiscountList  != undefined && options.thirdDiscountList.length != 0){
            renderThirdDisountTable();
            thirdDiscountTable.reload({
                data: options.thirdDiscountList,
            })
        } else {
            $("#noThird").text("无")
        }
        if (options.incrementDiscountList  != undefined && options.incrementDiscountList.length != 0){
            renderIncrementTable();
            incrementTable.reload({
                data: options.incrementDiscountList,
            })
        } else {
            $("#noIncrementDiscount").text("无")
        }
        if (options.userBindDiscountList  != undefined && options.userBindDiscountList.length != 0){
            renderUserBindDiscountTable()
            userBindTable.reload({
                data: options.userBindDiscountList,
            })
        } else {
            $("#noUserBindDiscount").text("无")
        }
    }

    function initEvent () {
    }

    function init () {
        initEvent();

        initPage();
    }

    init();

    exports("oms_order_showDiscount");
});
